package mce.web.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mce.system.configuration.Characteristic;
import mce.system.configuration.ProcessStep;
import mce.system.configuration.definition.MceMenu;
import mce.system.configuration.definition.QuoteStepCode;
import mce.system.constant.StringConstant;
import mce.system.model.Quote;
import mce.system.util.StringUtil;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/create-quote-simplex")
@SessionAttributes("quoteProcessModel")
public class QuoteController {
	private Logger log = Logger.getLogger(this.getClass().getName());
	
	@RequestMapping
	public ModelAndView defaultNewQuote(final ModelMap modelMap, HttpServletRequest request, HttpServletResponse response){
		log.info("Create quote simplex request");
		Quote quoteProcessModel = new Quote();
		quoteProcessModel.sortByLine();
		/**
		 * Call BAPI get information of Login User and set for model
		 * */
		//map.put(key, map.get(key) + 1);
		modelMap.addAttribute("quoteProcessModel",quoteProcessModel);
		return new ModelAndView("quote-process/simplex/create-quote", "quoteProcessModel", quoteProcessModel);
	}
	
	@RequestMapping(method = RequestMethod.POST)
    public ModelAndView processCreateQuoteStep(@RequestParam("navigateStep") final String navigateStep,
            final @ModelAttribute("quoteProcessModel") Quote quoteProcessModel, final HttpServletRequest request,
            final HttpServletResponse response) {
		String jobName="";
    	/**
    	 * Get list characteristic key indicated by quote process step
    	 * Update values for characteristics
    	 */
    	Iterator<String> iterKeys = quoteProcessModel.getCharacteristics().keySet().iterator();
    	while(iterKeys.hasNext()){
    		String characteristicKey = iterKeys.next();
    		Characteristic item = quoteProcessModel.getCharacteristics().get(characteristicKey);
    		item.set_DEFAULT_VALUE(request.getParameter(characteristicKey));
    		quoteProcessModel.getCharacteristics().put(characteristicKey, item);
    	}
    	if(quoteProcessModel.getCurrentStep().get_GROUP_NAME().equals(QuoteStepCode.JOB_INFORMATION_ID.getValue())){
    		jobName = request.getParameter(StringConstant.NSC_JOB_NAME_NAME);
    		request.getSession().setAttribute(StringConstant.NSC_JOB_NAME_NAME, jobName);
    	}
    	/**
    	 * Call BAPI and get steps
    	 */
    	quoteProcessModel.setSteps(new ArrayList<ProcessStep>());
		// Job information
		ProcessStep jobInformation = new ProcessStep();
		jobInformation.set_GROUP_NAME(QuoteStepCode.JOB_INFORMATION_ID.getValue());
		jobInformation.set_GROUP_TEXT(QuoteStepCode.JOB_INFORMATION_TEXT.getValue());
		jobInformation.set_TEXT_DESCRIPTION(StringConstant.CREATE_QUOTE_NOTIFICATION);
		jobInformation.set_JOBNAME(null);
		jobInformation.setHasBackButton(Boolean.FALSE);
		jobInformation.setBackButtonText(null);
		jobInformation.setHasNextButton(Boolean.TRUE);
		jobInformation.setNextButtonText(StringConstant.NEXT_BUTTON_DEFAULT);
		jobInformation.setBackStepCode(null);
		jobInformation.setNextStepCode(QuoteStepCode.SYSTEM_CONFIGURATION_ID.getValue());
		quoteProcessModel.getSteps().add(jobInformation);
		// System Configuration
		ProcessStep systemConfiguration = new ProcessStep();
		systemConfiguration.set_GROUP_NAME(QuoteStepCode.SYSTEM_CONFIGURATION_ID.getValue());
		systemConfiguration.set_GROUP_TEXT(QuoteStepCode.SYSTEM_CONFIGURATION_TEXT.getValue());
		systemConfiguration.set_TEXT_DESCRIPTION(null);
		systemConfiguration.set_JOBNAME((String)request.getSession().getAttribute(StringConstant.NSC_JOB_NAME_NAME));
		systemConfiguration.setHasBackButton(Boolean.TRUE);
		systemConfiguration.setBackButtonText(StringConstant.BACK_BUTTON_DEFAULT);
		systemConfiguration.setHasNextButton(Boolean.TRUE);
		systemConfiguration.setNextButtonText(StringConstant.NEXT_BUTTON_DEFAULT);
		systemConfiguration.setBackStepCode(QuoteStepCode.JOB_INFORMATION_ID.getValue());
		systemConfiguration.setNextStepCode("B");//Starter
		quoteProcessModel.getSteps().add(systemConfiguration);
		// Starter
		ProcessStep starter = new ProcessStep();
		starter.set_GROUP_NAME("B");
		starter.set_GROUP_TEXT("Starter");
		starter.set_TEXT_DESCRIPTION(null);
		starter.set_JOBNAME((String)request.getSession().getAttribute(StringConstant.NSC_JOB_NAME_NAME));
		starter.setHasBackButton(Boolean.TRUE);
		starter.setBackButtonText(StringConstant.BACK_BUTTON_DEFAULT);
		starter.setHasNextButton(Boolean.TRUE);
		starter.setNextButtonText(StringConstant.NEXT_BUTTON_DEFAULT);
		starter.setBackStepCode(QuoteStepCode.SYSTEM_CONFIGURATION_ID.getValue());
		starter.setNextStepCode("C");//Hoist way
		quoteProcessModel.getSteps().add(starter);
		// Hoist way
		ProcessStep hoistway = new ProcessStep();
		hoistway.set_GROUP_NAME("C");
		hoistway.set_GROUP_TEXT("Hoistway");
		hoistway.set_TEXT_DESCRIPTION(null);
		hoistway.set_JOBNAME((String)request.getSession().getAttribute(StringConstant.NSC_JOB_NAME_NAME));
		hoistway.setHasBackButton(Boolean.TRUE);
		hoistway.setBackButtonText(StringConstant.BACK_BUTTON_DEFAULT);
		hoistway.setHasNextButton(Boolean.TRUE);
		hoistway.setNextButtonText(StringConstant.NEXT_BUTTON_DEFAULT);
		hoistway.setBackStepCode("B");
		hoistway.setNextStepCode("D");//Hoist way
		quoteProcessModel.getSteps().add(hoistway);
		// Options
		ProcessStep options = new ProcessStep();
		options.set_GROUP_NAME("D");
		options.set_GROUP_TEXT("Options");
		options.set_TEXT_DESCRIPTION(null);
		options.set_JOBNAME((String)request.getSession().getAttribute(StringConstant.NSC_JOB_NAME_NAME));
		options.setHasBackButton(Boolean.TRUE);
		options.setBackButtonText(StringConstant.BACK_BUTTON_DEFAULT);
		options.setHasNextButton(Boolean.TRUE);
		options.setNextButtonText(StringConstant.NEXT_BUTTON_DEFAULT);
		options.setBackStepCode("C");
		options.setNextStepCode("R");//Review Summary
		quoteProcessModel.getSteps().add(options);
    	/**
    	 * Quote step indicated
    	 */
    	quoteProcessModel.setCurrentStep(quoteProcessModel.getSteps().get(quoteProcessModel.getStepByGroupName(navigateStep)));
    	/**
    	 * Generate MCE menu from quote process steps
    	 */
    	List<MceMenu> mceMenuGenerated = new ArrayList<MceMenu>();
    	if(quoteProcessModel.getCurrentStep().get_GROUP_NAME().equals(QuoteStepCode.JOB_INFORMATION_ID.getValue())){
    		/**
    		 * Generate menu default for first step
    		 */
    		mceMenuGenerated = new ArrayList<MceMenu>();
    		mceMenuGenerated.add(new MceMenu(QuoteStepCode.JOB_INFORMATION_ID
    				.getValue(), QuoteStepCode.JOB_INFORMATION_TEXT.getValue(), 0));
    		mceMenuGenerated.add(new MceMenu(QuoteStepCode.SYSTEM_CONFIGURATION_ID
    				.getValue(),
    				QuoteStepCode.SYSTEM_CONFIGURATION_TEXT.getValue(), 0));
    		mceMenuGenerated.add(new MceMenu(QuoteStepCode.CAR_CONFIGURATION_ID.getValue(),
    						QuoteStepCode.CAR_CONFIGURATION_TEXT.getValue(), 0));
    		mceMenuGenerated.add(new MceMenu(QuoteStepCode.RECEIVE_QUOTE_ID.getValue(),
    				QuoteStepCode.RECEIVE_QUOTE_TEXT.getValue(), 0));
    		Collections.sort(mceMenuGenerated, StringUtil.alphabeticalOrder);
    		quoteProcessModel.setMceMenu(mceMenuGenerated);
    	}else if(quoteProcessModel.getCurrentStep().get_GROUP_NAME().equals(QuoteStepCode.SYSTEM_CONFIGURATION_ID.getValue())){
    		/**
    		 * Generate menu default for second step
    		 */
    		mceMenuGenerated = new ArrayList<MceMenu>();
    		mceMenuGenerated.add(new MceMenu(QuoteStepCode.JOB_INFORMATION_ID
    				.getValue(), QuoteStepCode.JOB_INFORMATION_TEXT.getValue(), 0));
    		mceMenuGenerated.add(new MceMenu(QuoteStepCode.SYSTEM_CONFIGURATION_ID
    				.getValue(),
    				QuoteStepCode.SYSTEM_CONFIGURATION_TEXT.getValue(), 0));
    		mceMenuGenerated.add(new MceMenu(QuoteStepCode.CAR_CONFIGURATION_ID.getValue(),
    						QuoteStepCode.CAR_CONFIGURATION_TEXT.getValue(), 0));
    		mceMenuGenerated.add(new MceMenu(QuoteStepCode.RECEIVE_QUOTE_ID.getValue(),
    				QuoteStepCode.RECEIVE_QUOTE_TEXT.getValue(), 0));
    		mceMenuGenerated.add(new MceMenu(QuoteStepCode.REVIEW_SUMMARY_ID.getValue(),
    				QuoteStepCode.REVIEW_SUMMARY_TEXT.getValue(), 0));
    		Collections.sort(mceMenuGenerated, StringUtil.alphabeticalOrder);
    		quoteProcessModel.setMceMenu(mceMenuGenerated);
    	}else{
    		for (ProcessStep proStep : quoteProcessModel.getSteps()) {
    			MceMenu menu = new MceMenu();
    			if(!proStep.get_GROUP_NAME().equals(QuoteStepCode.JOB_INFORMATION_ID.getValue())&&
    					!proStep.get_GROUP_NAME().equals(QuoteStepCode.SYSTEM_CONFIGURATION_ID.getValue())){
    				menu.set_PADDING_LEFT(15);
    			}else{
    				menu.set_PADDING_LEFT(0);
    			}
    			menu.set_GROUP_TEXT(proStep.get_GROUP_TEXT());
    			menu.set_GROUP_NAME(proStep.get_GROUP_NAME());
    			mceMenuGenerated.add(menu);
    		}
        	/**
        	 * Add steps "Review Summary ", "Receive Quote", "Car Configuration";
        	 */
        	mceMenuGenerated.add(new MceMenu(QuoteStepCode.REVIEW_SUMMARY_ID.getValue(),
        			QuoteStepCode.REVIEW_SUMMARY_TEXT.getValue(), 0));
        	mceMenuGenerated.add(new MceMenu(QuoteStepCode.RECEIVE_QUOTE_ID.getValue(),
        			QuoteStepCode.RECEIVE_QUOTE_TEXT.getValue(), 0));
        	mceMenuGenerated.add(new MceMenu(QuoteStepCode.CAR_CONFIGURATION_ID.getValue(),
        			QuoteStepCode.CAR_CONFIGURATION_TEXT.getValue(), 0));
        	Collections.sort(mceMenuGenerated, StringUtil.alphabeticalOrder);
    		quoteProcessModel.setMceMenu(mceMenuGenerated);
    	}
    	return new ModelAndView("quote-process/simplex/create-quote","quoteProcessModel",quoteProcessModel);
    }
}
